package kofman.test.cps;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import kofman.test.cps.TableAdapter.TableCell;
import kofman.test.cps.TableAdapter.TableRow;


import android.app.Activity;

import android.os.Bundle;
import android.view.Menu;

import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class listViewActivity extends Activity {
	protected static final int menu_button_1=Menu.FIRST+1;
    ListView lv;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        this.setTitle("ListView自适应实现表格");  
        lv = (ListView) this.findViewById(R.id.ListView01);  
        ArrayList<TableRow> table = new ArrayList<TableRow>();  
        TableCell[] titles = new TableCell[5];// 每行5个单元  
        int width = this.getWindowManager().getDefaultDisplay().getWidth()/titles.length;  
        // 定义标题  
        for (int i = 0; i < titles.length; i++) {  
            titles[i] = new TableCell("标题" + String.valueOf(i),   
                                    width + 8 * i,  
                                    LayoutParams.FILL_PARENT,   
                                    TableCell.STRING);  
        }  
        table.add(new TableRow(titles));  
        // 每行的数据  
        TableCell[] cells = new TableCell[5];// 每行5个单元  
        for (int i = 0; i < cells.length - 1; i++) {  
            cells[i] = new TableCell("No." + String.valueOf(i),  
                                    titles[i].width,   
                                    LayoutParams.FILL_PARENT,   
                                    TableCell.STRING);  
        }  
        cells[cells.length - 1] = new TableCell(R.drawable.ic_launcher,  
                                                titles[cells.length - 1].width,   
                                                LayoutParams.WRAP_CONTENT,  
                                                TableCell.IMAGE);  
        // 把表格的行添加到表格  
        for (int i = 0; i < 12; i++){  
	        table.add(new TableRow(cells));
        }
	        TableAdapter tableAdapter = new TableAdapter(this, table);  
	        lv.setAdapter(tableAdapter);
	        lv.setOnItemClickListener(new ItemClickEvent());
    }
    //建立menu選單
    public boolean onCreateOptionsMenu(Menu menu){
    	menu.add(0,0,0,"說明");
    	menu.add(0,1,1,"離開");
    	return super.onCreateOptionsMenu(menu);
    }
    
    
    class ItemClickEvent implements OnItemClickListener {  


		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Toast.makeText(listViewActivity.this, "选中第"+String.valueOf(arg2)+"行", 500).show();  
        
			
		}  
    }  
} 

